/*
 * Copyright (c) Created by T C L on 2019.2.27.
 * 技师Adapter
 */

package com.egg.ylt.adapter;

import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.egg.ylt.R;
import com.egg.ylt.Utils.GlideCircleTransform;
import com.egg.ylt.pojo.TechnicianEntity;

import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ADA_TechnicianAdapter extends BaseAdapter {

    private List<TechnicianEntity.ListEntity> mTechnicianEntities;

    private OnTechnicianCheckedListener mOnTechnicianCheckedListener;

    public void setData(List<TechnicianEntity.ListEntity> technicianEntities) {
        mTechnicianEntities = technicianEntities;
        notifyDataSetChanged();
    }


    public ADA_TechnicianAdapter() {
        mTechnicianEntities = new ArrayList<>();
    }

    public interface OnTechnicianCheckedListener {
        void onTechnicianChecked(String technicianId, String technicianName);
    }

    public void setOnTechnicianCheckedListener(OnTechnicianCheckedListener onTechnicianCheckedListener) {
        this.mOnTechnicianCheckedListener = onTechnicianCheckedListener;
    }


    @Override
    public int getCount() {
        return mTechnicianEntities == null ? 0 : mTechnicianEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return mTechnicianEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final TechnicianViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.item_technician, null);
            holder = new TechnicianViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (TechnicianViewHolder) convertView.getTag();
        }

        final TechnicianEntity.ListEntity entity = mTechnicianEntities.get(position);

        Glide.with(parent.getContext())
                .load(entity.getHeadUrl())
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.drawable.healthdata_icon_headportrait_default)
                .error(R.drawable.healthdata_icon_headportrait_default)
                .transform(new GlideCircleTransform(parent.getContext()))
                .dontAnimate()
                .into(holder.mTechnicianHeadImgIv);


        if (entity.getTechnicianStatus().equals("0")) {
            holder.mTechnicianStatusTv.setText("空闲");
            holder.mTechnicianStatusTv.setTextColor(Color.parseColor("#FF12CB77"));
        } else if (entity.getTechnicianStatus().equals("1")) {
            holder.mTechnicianStatusTv.setText("忙碌");
            holder.mTechnicianStatusTv.setTextColor(Color.parseColor("#FFEF382D"));
        } else {
            holder.mTechnicianStatusTv.setText("");
        }

        holder.mTechnicianNameTv.setText(entity.getTechnicianName());

        holder.mTechnicianDescriptionTv.setText(entity.getRemark());

        if (entity.isChecked()) {
            holder.mCheckedIv.setVisibility(View.VISIBLE);
        } else {
            holder.mCheckedIv.setVisibility(View.GONE);
        }


        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //可选
                if (entity.getTechnicianStatus().equals("0")) {
                    if (entity.isChecked()) {
                        holder.mCheckedIv.setVisibility(View.GONE);
                        entity.setChecked(false);
                    } else {

                        if (mOnTechnicianCheckedListener != null) {
                            mOnTechnicianCheckedListener.onTechnicianChecked(entity.getTechnicianId(),
                                    entity.getTechnicianName());
                        }

                        holder.mCheckedIv.setVisibility(View.VISIBLE);
                        entity.setChecked(true);


                        for (int i = 0; i < mTechnicianEntities.size(); i++) {
                            if (position != i) {
                                mTechnicianEntities.get(i).setChecked(false);
                            }
                        }
                    }
                    notifyDataSetChanged();
                }
            }
        });
        return convertView;
    }


    class TechnicianViewHolder {
        @BindView(R.id.technician_head_img_iv)
        ImageView mTechnicianHeadImgIv;
        @BindView(R.id.technician_status_tv)
        TextView mTechnicianStatusTv;
        @BindView(R.id.technician_name_tv)
        TextView mTechnicianNameTv;
        @BindView(R.id.technician_description_tv)
        TextView mTechnicianDescriptionTv;
        @BindView(R.id.checked_iv)
        ImageView mCheckedIv;

        public TechnicianViewHolder(View itemView) {
            ButterKnife.bind(this, itemView);
        }
    }
}
